import useAppStore from '@ide/store';
import { getTreeNode } from '@ide/utils/tools';
import { useIdeStore } from 'ide-plugin';
import { EditorItem } from 'ide-plugin/dist/ide/store/editorStore';
import { useCallback } from 'react';

export const useAppOpenEditor = () => {
  const { openEditor } = useIdeStore();
  const { fileTree } = useAppStore();
  const appOpenEditor = useCallback((item:EditorItem) => {
    const node = getTreeNode(item.path, fileTree);
    if (!node) {
      return;
    }
    openEditor({
      ...item,
      editable: node.editable
    });
  },[fileTree]);
  return {
    appOpenEditor
  };
};
